package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.client.protocol.convertor.StringToListConvertor;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    @LcnTransaction
    public void importOrderSettings(List<String[]> list) {
        //解析excel表格数据，转换成java对象
        List<OrderSetting>  orderSettings = StringTOList(list);
        //存放么有id的预约对象
        ArrayList<OrderSetting> orderSettingSave = new ArrayList<>();
        //存放有id的预约对象
        ArrayList<OrderSetting> orderSettingUpdate = new ArrayList<>();
        //  循环遍历集合  查询数据库表中有无 当前日期记录
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            QueryWrapper<OrderSetting> wrapper = new QueryWrapper<>();
            wrapper.eq("ORDERDATE",orderDate);
            OrderSetting selectOne = baseMapper.selectOne(wrapper);
            if(selectOne!=null){
                //  批量修改  需要将id  封装到List集合中
                orderSetting.setId(selectOne.getId());//     查询id  封装到 添加OrderSetting 对象
                orderSettingUpdate.add(orderSetting);
            }else{
                orderSettingSave.add(orderSetting);
            }
        }
        saveBatch(orderSettingSave);
        updateBatchById(orderSettingUpdate);
    }

    @Override
    public Map findSettingData(String year, String month) {

        //   "2020-09-28":{number:100,reservations:90}
        String  beginDate  = year+"-"+month+"-1";
        String  endDate  =  year+"-"+month+"-31";
        List<Map> mapList = baseMapper.findSettingData(beginDate,endDate);
        HashMap<String, OrderSetting> hashMap = new HashMap<>();
        for (Map map : mapList) {
            OrderSetting orderSetting = new OrderSetting();
            Integer number = (Integer) map.get("number");
            Integer reservations = (Integer) map.get("RESERVATIONS");
            Date orderDate = (Date) map.get("ORDERDATE");
            orderSetting.setNumber(number);
            orderSetting.setReservations(reservations);
            String date = DateUtils.parseDate2String(orderDate,"yyyy-MM-dd");
            hashMap.put(date,orderSetting);

        }
        return hashMap;
    }

    @Override
    @LcnTransaction
    public Boolean updateNumberByOrderdate(int number, String orderDate) {

      return   baseMapper.updateNumberByOrderdate(number,orderDate);
    }

    @Override
    public int isOrderOk(String orderDate) {
       int count = baseMapper.isOrderOk(orderDate);
        return count;
    }

    @Override
    @LcnTransaction
    public void updateReservetionsByOrderDate(String orderDate) {
        baseMapper.updateReservetionsByOrderDate(orderDate);
    }

    private List<OrderSetting> StringTOList(List<String[]> list) {
        if (list != null && list.size() > 0) {
            ArrayList<OrderSetting> orderSettingsList = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setReservations(0);
                orderSettingsList.add(orderSetting);
            }
            return orderSettingsList;
        }else {
            return null;
        }

    }
}
